#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 100 + 1, maxk = 5, maxs = 1000 + 1;
int T,n,s;
int rp[maxs] ; 					// The preson who is reading the book
int log[maxs][maxn] = {0};

int booklog[maxs];

struct Person
{
	int at, s;							// at: When the person arrived at the library
	int id[maxk];					// The id of the book
	int t[maxk];					// How long the person should read this book
	bool isr[maxk];				// Whether the preson have read the book
	int rb;								// Which book the preson is reading
	int wt;								// How long preson should work.
	int logt;
}p[maxn] = {0};

int main()
{
	freopen("reading.in","r",stdin);
	freopen("reading.out","w",stdout);
	int i,j;

	memset(rp, 0xffffffff, sizeof(rp));
	memset(booklog, 0xffffffff, sizeof(booklog));
	cin >> T >> n;
	s = 0;
	for(int i = 0; i < n ; i ++)
	{
		cin >> p[i].at >> p[i].s;
		p[i].rb = -1;
		p[i].wt = 0;
		p[i].logt = 0;
		for(int j = 0; j < p[i].s ; j ++)
		{
			cin >> p[i].id[j] >> p[i].t[j];
			if(p[i].id[j] >= s)
				s = p[i].id[j] + 1;
		}
	}
	//cout << s << endl;

	int total = 0;

	for(int i = 0; i <= T; i ++)
	{
		//cout << "time:" << i << endl;
		for(int j = 0; j < n; j ++)
		{
			if(p[j].at > i)
				continue;
			//cout << j << ' ' << p[j].wt << endl;
			if(p[j].wt == 0)
			{
				bool isl = 1;
				for(int k = 0; k < p[j].s; k ++)
					if(!p[j].isr[k] && rp[p[j].id[k]] == -1)
					{
						//cout << p[j].id[k] << "empty" << endl;
						isl = 0;
						break;
					}
				if(isl && p[j].logt == 0)
				{
					//cout << " log" << endl;
					p[j].logt = i;
					//if(booklog[i])
					for(int k = 0; k < p[j].s; k ++)
						if(!p[j].isr[k] && rp[p[j].id[k]] != -1)
						{
							if(booklog[p[j].id[k]] == -1)
							{
								booklog[p[j].id[k]] = j;
							}
							else if(p[booklog[p[j].id[k]]].logt > p[j].logt)
							{
								booklog[p[j].id[k]] = j;
							}
							else if (p[booklog[p[j].id[k]]].logt == p[j].logt && p[booklog[p[j].id[k]]].at > p[j].at)
							{
								booklog[p[j].id[k]] = j;
							}
						}
				}
				else
				for(int k = 0; k < p[j].s; k ++)
					if(!p[j].isr[k] && rp[p[j].id[k]] == -1)
					{
						 if(booklog[p[j].id[k]] == j || booklog[p[j].id[k]] == -1)
						 {
								p[j].rb = k;
								p[j].wt = p[j].t[k];
								rp[p[j].id[k]] = j;
								p[j].logt = 0;
								booklog[p[j].id[k]] = -1;
								if(i < T)
								total ++;
								//cout << j << " select:" << p[j].id[k] << " wt:" << p[j].wt <<endl;
								break;
						 }
					}
			}
		}

		for(int j = 0; j < n; j ++)
		{
			if(p[j].wt > 0)
				p[j].wt --;
			if(p[j].wt == 0)
			{
				if(p[j].rb != -1)
				{
					p[j].isr[p[j].rb] = 1;
					rp[p[j].id[p[j].rb]] = -1;
					//cout << j << " finish:" <<  p[j].id[p[j].rb] << endl;
					p[j].rb = -1;
				}
			}
		}/*
		for(int j = 0; j < s; j ++)
			cout << booklog[j] << endl;
		cout << endl;*/
	}

	cout << total << endl;

	return 0;
}
